
 ; =====================================================================================
 ;
 ;       Filename:  idt_func.s
 ;
 ;    Description:  banana 内核 intr init,setup idt table and intr functions
 ;
 ;        Version:  1.0
 ;        Created:  2015年1月2日
 ;       Revision:  none
 ;       Compiler:  gcc
 ;
 ;         Author:  shao,banana family 
 ;        Company:  GrenTech
 ;
 ; =====================================================================================
 ;
%include    "./boot/idt.inc"

[GLOBAL IDT_START]
var_IDT0:	INT_GATE_DESC	0x100000,0x08,0x8e00
var_IDT1:	INT_GATE_DESC	0x100000,0x08,0x8e00
var_IDT2:	INT_GATE_DESC	0x100000,0x08,0x8e00
var_IDT3:	INT_GATE_DESC	0x100000,0x08,0x8e00
var_IDT4:	INT_GATE_DESC	0x100000,0x08,0x8e00
var_IDT5:	INT_GATE_DESC	0x100000,0x08,0x8e00
var_IDT6:	INT_GATE_DESC	0x100000,0x08,0x8e00
var_IDT7:	INT_GATE_DESC	0x100000,0x08,0x8e00
var_IDT8:	INT_GATE_DESC	0x100000,0x08,0x8e00
var_IDT9:	INT_GATE_DESC	0x100000,0x08,0x8e00
var_IDT10:	INT_GATE_DESC	0x100000,0x08,0x8e00
var_IDT11:	INT_GATE_DESC	0x100000,0x08,0x8e00
var_IDT12:	INT_GATE_DESC	0x100000,0x08,0x8e00
var_IDT13:	INT_GATE_DESC	0x100000,0x08,0x8e00
var_IDT14:	INT_GATE_DESC	0x100000,0x08,0x8e00
var_IDT15:	INT_GATE_DESC	0x100000,0x08,0x8e00
var_IDT16:	INT_GATE_DESC	0x100000,0x08,0x8e00
var_IDT17:	INT_GATE_DESC	0x100000,0x08,0x8e00
var_IDT18:	INT_GATE_DESC	0x100000,0x08,0x8e00
var_IDT19:	INT_GATE_DESC	0x100000,0x08,0x8e00
var_IDT20:	INT_GATE_DESC	0x100000,0x08,0x8e00
var_IDT21:	INT_GATE_DESC	0x100000,0x08,0x8e00
var_IDT22:	INT_GATE_DESC	0x100000,0x08,0x8e00
var_IDT23:	INT_GATE_DESC	0x100000,0x08,0x8e00
var_IDT24:	INT_GATE_DESC	0x100000,0x08,0x8e00
var_IDT25:	INT_GATE_DESC	0x100000,0x08,0x8e00
var_IDT26:	INT_GATE_DESC	0x100000,0x08,0x8e00
var_IDT27:	INT_GATE_DESC	0x100000,0x08,0x8e00
var_IDT28:	INT_GATE_DESC	0x100000,0x08,0x8e00
var_IDT29:	INT_GATE_DESC	0x100000,0x08,0x8e00
var_IDT30:	INT_GATE_DESC	0x100000,0x08,0x8e00
var_IDT31:	INT_GATE_DESC	0x100000,0x08,0x8e00

var_IDT32:	INT_GATE_DESC	0x100000,0x08,0x8e00

IdtLen 	equ 	$-var_IDT0
IdtPtr 	dw	(IdtLen-1)
	dd	0
%macro ISR_NOERRCODE 1
[GLOBAL isr%1]
isr%1:
	cli                        
	push 0                      
	push %1                     
	jmp isr_common_stub
%endmacro

%macro ISR_ERRCODE 1
[GLOBAL isr%1]
isr%1:
	cli                        
	push %1                     
	jmp isr_common_stub
%endmacro	
		
; 定义中断处理函数
ISR_NOERRCODE  0 	; 0 #DE 除 0 异常
ISR_NOERRCODE  1 	; 1 #DB 调试异常
ISR_NOERRCODE  2 	; 2 NMI
ISR_NOERRCODE  3 	; 3 BP 断点异常 
ISR_NOERRCODE  4 	; 4 #OF 溢出 
ISR_NOERRCODE  5 	; 5 #BR 对数组的引用超出边界 
ISR_NOERRCODE  6 	; 6 #UD 无效或未定义的操作码 
ISR_NOERRCODE  7 	; 7 #NM 设备不可用(无数学协处理器) 
ISR_ERRCODE    8 	; 8 #DF 双重故障(有错误代码) 
ISR_NOERRCODE  9 	; 9 协处理器跨段操作
ISR_ERRCODE   10 	; 10 #TS 无效TSS(有错误代码) 
ISR_ERRCODE   11 	; 11 #NP 段不存在(有错误代码) 
ISR_ERRCODE   12 	; 12 #SS 栈错误(有错误代码) 
ISR_ERRCODE   13 	; 13 #GP 常规保护(有错误代码) 
ISR_ERRCODE   14 	; 14 #PF 页故障(有错误代码) 
ISR_NOERRCODE 15 	; 15 CPU 保留 
ISR_NOERRCODE 16 	; 16 #MF 浮点处理单元错误 
ISR_ERRCODE   17 	; 17 #AC 对齐检查 
ISR_NOERRCODE 18 	; 18 #MC 机器检查 
ISR_NOERRCODE 19 	; 19 #XM SIMD(单指令多数据)浮点异常

; 20~31 Intel 保留
ISR_NOERRCODE 20
ISR_NOERRCODE 21
ISR_NOERRCODE 22
ISR_NOERRCODE 23
ISR_NOERRCODE 24
ISR_NOERRCODE 25
ISR_NOERRCODE 26
ISR_NOERRCODE 27
ISR_NOERRCODE 28
ISR_NOERRCODE 29
ISR_NOERRCODE 30
ISR_NOERRCODE 31

ISR_NOERRCODE 32

[EXTERN isr_handler]
isr_common_stub:
	pusha                    ; Pushes edi, esi, ebp, esp, ebx, edx, ecx, eax
	mov ax, ds
	push eax                
	
	mov ax, 0x10
	mov ds, ax
	mov es, ax
	mov fs, ax
	mov gs, ax
	mov ss, ax
	
	push esp	
	call isr_handler       
	add esp, 4 		
	
	pop ebx                
	mov ds, bx
	mov es, bx
	mov fs, bx
	mov gs, bx
	mov ss, bx
	
	popa                     ; Pops edi, esi, ebp, esp, ebx, edx, ecx, eax
	add esp, 8               
	iret

IDT_START:
		mov	eax,isr0
		mov	word [var_IDT0],ax
		mov	eax,isr1
		mov	word [var_IDT1],ax
		mov	eax,isr2
		mov	word [var_IDT2],ax
		mov	eax,isr3
		mov	word [var_IDT3],ax
		mov	eax,isr4
		mov	word [var_IDT4],ax
		mov	eax,isr5
		mov	word [var_IDT5],ax
		mov	eax,isr6
		mov	word [var_IDT6],ax
		mov	eax,isr7
		mov	word [var_IDT7],ax
		mov	eax,isr8
		mov	word [var_IDT8],ax
		mov	eax,isr9
		mov	word [var_IDT9],ax
		mov	eax,isr10
		mov	word [var_IDT10],ax
		mov	eax,isr11
		mov	word [var_IDT11],ax
		mov	eax,isr12
		mov	word [var_IDT12],ax
		mov	eax,isr13
		mov	word [var_IDT13],ax
		mov	eax,isr14
		mov	word [var_IDT14],ax
		mov	eax,isr15
		mov	word [var_IDT15],ax
		mov	eax,isr16
		mov	word [var_IDT16],ax
		mov	eax,isr17
		mov	word [var_IDT17],ax
		mov	eax,isr18
		mov	word [var_IDT18],ax
		mov	eax,isr19
		mov	word [var_IDT19],ax
		mov	eax,isr20
		mov	word [var_IDT20],ax
		mov	eax,isr21
		mov	word [var_IDT21],ax
		mov	eax,isr22
		mov	word [var_IDT22],ax
		mov	eax,isr23
		mov	word [var_IDT23],ax
		mov	eax,isr24
		mov	word [var_IDT24],ax
		mov	eax,isr25
		mov	word [var_IDT25],ax
		mov	eax,isr26
		mov	word [var_IDT26],ax
		mov	eax,isr27
		mov	word [var_IDT27],ax
		mov	eax,isr28
		mov	word [var_IDT28],ax
		mov	eax,isr29
		mov	word [var_IDT29],ax
		mov	eax,isr30
		mov	word [var_IDT30],ax
		mov	eax,isr31
		mov	word [var_IDT31],ax
		
		mov	eax,isr32
		mov	word [var_IDT32],ax
		
		mov 	eax,var_IDT0
		mov 	dword [IdtPtr+2],eax
		lidt	[IdtPtr]
		ret	

